/*
 * ResourceStatus.java
 *
 * Created on May 8, 2009, 1:24 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package im.resources;

import im.general.Config;
import im.numgenerator.PseudoRandomGenerator;
import im.resources.model.ResourcesModel;
import java.io.Serializable;

/**
 *
 * @author bluejack
 */
public class ResourceStatus implements Serializable {
    
    /** Creates a new instance of ResourceStatus */
    //TODO non devono viaggiare tutte queste informazioni se le decisioni vengono prese in base al solo indexstatus
    private double statusIndex;
    private boolean plugged;
    private double chargeReminderMah;
    private double chargeReminderRatio;
    private double freeBWKbps;

    private int freeCPUMhz;
    private int freeDiskMB;
    private int freeRamMB;
    private String container;
/*
    public ResourceStatus() {
        
    }
 * */

    public ResourceStatus(ResourcesModel rm, String container) {
        updateStatus(rm);
        this.container = container;
    }

    public void updateStatus(ResourcesModel rm) {
        chargeReminderMah = rm.getChargeReminderMah();
        chargeReminderRatio = 1.0 - rm.getUsedBatteryRatio();
        freeBWKbps = rm.getFreeBWKbps();
        freeCPUMhz = rm.getFreeCPUMhz();
        freeDiskMB = rm.getFreeDiskMB();
        freeRamMB  = rm.getFreeRamMB();
        plugged    = rm.isIsFixPlugged();
        statusIndex = updateStatusIndex();
    }
    
    public double updateStatusIndex() {
        statusIndex = 0.0;
        statusIndex += (plugged)? Config.STAT_IDX_PLUGGED_WEIGHT : Config.STAT_IDX_CHARGE_WEIGHT * chargeReminderMah;
        statusIndex += Config.STAT_IDX_BANDW_WEIGHT * freeBWKbps;
        statusIndex += Config.STAT_IDX_CPU_WEIGHT * freeCPUMhz;
        statusIndex += Config.STAT_IDX_DISK_WEIGHT * freeDiskMB;
        statusIndex += Config.STAT_IDX_RAM_WEIGHT * freeRamMB;

        return statusIndex;
    }

    public double getChargeReminderMah() {
        return chargeReminderMah;
    }

    public double getFreeBWKbps() {
        return freeBWKbps;
    }

    public int getFreeCPUMhz() {
        return freeCPUMhz;
    }

    public int getFreeDiskMB() {
        return freeDiskMB;
    }

    public int getFreeRamMB() {
        return freeRamMB;
    }

    public boolean isBetter(ResourceStatus resourceStatus) {
        return this.getStatusIndex() > resourceStatus.getStatusIndex();
        //return resourceStatus.getStatusIndex() > this.getStatusIndex();
    }

    public boolean isLow() {
        //System.out.println("CHARGE: "+chargeReminderRatio);
        //return Math.random()<0.1;
        //return chargeReminderRatio < Config.IS_LOW_RATIO_THRSHOLD;//(Math.random()<0.25);
        return (this.getStatusIndex() < 200);
    }

    /*
    public void setStatus(float status) {
        this.status = status;
    }
     * */

    public boolean isPlugged() {
        return plugged;
    }

    public void setPlugged(boolean plugged) {
        this.plugged = plugged;
    }

    /**
     * @return the container
     */
    public String getContainer() {
        return container;
    }

    @Override
    public String toString() {
        return ""+statusIndex;
    }

    public double getStatusIndex() {
        return statusIndex;
    }
}
